package top.shiyiri.shops.controller;

import com.alibaba.fastjson.JSONObject;
import top.shiyiri.shops.bean.TimeSexCount;
import top.shiyiri.shops.service.TimeSexCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Aunean
 * @date 2021/12/27
 */
@Controller
public class TimeSexCountController {

    @Autowired
    private TimeSexCountService service;

    @RequestMapping("/timeSexCountByPage")
    @ResponseBody
    public String timeSexCountByPage(int page, HttpSession session) {
        List<TimeSexCount> list = service.timeSexCountByPage(page, 4);

        int lines = service.totalLines();
        int pages = lines % 4 == 0? lines / 4: lines / 4 + 1;

        session.setAttribute("pages", pages);
        session.setAttribute("page", page);

        return JSONObject.toJSONString(list);
    }

    @RequestMapping("/getTimeSexCountPages")
    @ResponseBody
    public Integer getTimeSexCountPages(HttpSession session) {

        int pages = (Integer) session.getAttribute("pages");
        return pages;
    }


    @RequestMapping("/timeSexCount")
    @ResponseBody
    public String timeSexCount() {
        List<TimeSexCount> list = service.timeSexCount();

        return JSONObject.toJSONString(list);
    }
}
